package by.wind.drebedengi.datalayer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;
import java.util.Map.Entry;

import by.wind.drebedengi.datalayer.bean.Place;
import by.wind.drebedengi.datalayer.bean.Place;

public class GetPlaceList extends AbstractAction {

	public static final String SOAP_ACTION = "getPlaceList";
	public static final String METHOD_NAME = "getPlaceList";
	
	private ArrayList<Place> itsPlaceList = new ArrayList<Place>();
	
	
	@Override
	public void loadData() throws Exception{
		HashMap<String, Object> propertyList = new HashMap<String, Object>();
		propertyList.put(Constants.API_ID_KEY, Constants.API_ID_VALUE);
		propertyList.put(Constants.LOGIN_KEY, Constants.LOGIN_VALUE);
		propertyList.put(Constants.PASS_KEY, Constants.PASS_VALUE);
		
		Vector<Hashtable<String,String>> itsData = (Vector<Hashtable<String,String>>) loadData(METHOD_NAME, SOAP_ACTION, propertyList);
		for(int i = 0 ; i< itsData.size() ; i ++) {
			itsPlaceList.add(convertHashtableToPlace(itsData.get(i)));
		}
	}


	private Place convertHashtableToPlace(Hashtable<String, String> element) {
		
		Place placeElement = new Place();
		Iterator<Entry<String, String>> iterator = element.entrySet().iterator();
		while(iterator.hasNext()) {
			
			String key = iterator.next().getKey();
			String value = element.get(key);
			if (key.equals(Place.ID)) {
				placeElement.setServer_id(Long.parseLong(value));
			} else if (key.equals(Place.ICON_ID)) {
				placeElement.setIcon_id(Long.parseLong(value));
			} else if (key.equals(Place.IS_FOR_DUTY)) {
				if (value.equals("t"))
					placeElement.setIs_for_duty(true);
				else
					placeElement.setIs_for_duty(false);
			} else if (key.equals(Place.NAME)) {
				placeElement.setName(value);
			} else if (key.equals(Place.SORT)) {
				placeElement.setSort(Long.valueOf(value));
			} else if (key.equals(Place.TYPE)) {
					placeElement.setType(Integer.parseInt(value));
			} else if (key.equals(Place.BUDGET_FAMILY_ID)) {
				    placeElement.setBudget_family_id(Long.parseLong(value));
			} else if (key.equals(Place.IS_HIDDEN)) {
				if (value.equals("t")) {
					placeElement.setIs_hidden(true);
				}
			}
			
		}
		
		return placeElement;
	}
	
	public ArrayList<Place> getPlaceList() {
		return itsPlaceList;
	}

}
